<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
         $cat = D('Cat');
         $cats = $cat ->getTree();
         $this->assign('cats',$cats);


         $goods = D('Goods');
         $goods ->where('is_best=1')->order('goods_id desc')->limit(0,3);
         $this->assign('best',$goods->select());



         $goods->where('is_hot=1')->order('goods_id desc')->limit(0,3);
         $this->assign('hot',$goods->select());


         $goods->where('is_new=1')->order('goods_id desc')->limit(0,3);
         $this->assign('new',$goods->select());


        $this->assign('history',array_reverse(session('history'),true));

        $this->display();
    }



    public function cat(){
    	$cat = D('Cat');
    	$cats = $cat->getTree();
    	$this->assign('cats',$cats);


    	$goods = D('Goods');

          $count      = $goods->where('cat_id='.I('get.cat_id'))->count('*');// 查询满足要求的总记录数
          $Page       = new \Think\Page($count,9);// 实例化分页类 传入总记录数和每页显示的记录数(25)
          $show       = $Page->show();// 分页显示输出
          // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
          $goods ->where('cat_id='.I('get.cat_id'));
          $goods->order('goods_id desc')->limit($Page->firstRow,$Page->listRows);

          $this->assign('goods',$goods->select());// 赋值数据集
          $this->assign('page',$show);// 赋值分页输出

    	//$goods->where('cat_id='.I('get.cat_id'));
          //$goods->order('goods_id desc')->limit(0,9);
          //$this->assign('goods',$goods->select());

          $this->display();
    }




    public function goods(){
        $goods = D('Goods');
        $goods = $goods->find(I('get.goods_id'));
        $this->assign('goods',$goods);


        $cat = D('Cat');
        //print_r($cat->getMbx($goods['cat_id']));
        $this->assign('cats',$cat->getMbx($goods['cat_id']));


        //把商品写入历史记录里面
       $this->history($goods);

       print_r(session('history'));

        $this->display();
    }


    public function history($goods){
      $history = session('history');

      if(empty($history)){
        $history = array();
      }

      if(isset($history[$goods['goods_id']])){
        unset($history[$goods['goods_id']]);
      }

      $row = array();
      $row['thumb_img'] = $goods['thumb_img'];
      $row['goods_name'] = $goods['goods_name'];
      $row['shop_price'] = $goods['shop_price'];

      $history[$goods['goods_id']] = $row;


    //浏览历史不超过6个
      if(count($history)>6){
         $key = key($history);//key为系统函数：获取第1个单元的键
         unset($history[$key]);
      }


      session('history',$history);
    }



    public function test(){
      //print_r(new \Home\Tool\CartTool());
      //$cart = D('Cart','Tool');
      $cart = \Home\Tool\CartTool::getIns();
      $cart ->add(3,'xiaomi',30);
      $cart ->add(3,'xiaomi',30);
      $cart ->add(5,'huawei',60);
      print_r($cart ->items());
    }



    public function test1(){
      //$cart = D('Cart','Tool');
      $cart = \Home\Tool\CartTool::getIns();
      print_r($cart ->items());
    }
    
}